package com.hopu.v1.controller.admin.classes;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.v1.pojo.classes.Student;
import com.hopu.v1.pojo.common.R;
import com.hopu.v1.pojo.common.Update;
import com.hopu.v1.service.admin.classes.StudentService;
import com.hopu.v1.service.admin.classes.StudentVoucherService;
import com.hopu.v1.vo.EmploymentInfoVo;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 学生管理
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    
    @Autowired
    private StudentService studentService;

    @Autowired
    private StudentVoucherService studentVoucherService;

    /**
     * 学生列表
     *
     * @param pages
     * @param student
     * @return
     */
    @GetMapping("/selectAll")
    public R selectAll(Page<Student> pages, Student student) {
        System.out.println(pages.getCurrent());
        System.out.println(pages.getSize());
        return R.ok("获取成功", studentService.selectAll(pages, student));
    }

    /**
     * 获取最后一条数据
     * @return
     */
    @GetMapping("/getLast")
    public R getLast() {
        return R.ok(studentService.getLastStudent());
    }

    /**
     * 通过学号获取学生
     */
    @GetMapping("/studentInfo")
    public R studentInfo(String studentId) {
        return R.ok("查询成功", this.studentService.studentInfo(studentId));
    }

    /**
     * 通过手机号获取学生
     */
    @GetMapping("/studentInfo/phone")
    public R studentInfoByPhone(String telephone) {
        return R.ok("查询成功", this.studentService.studentInfoByphone(telephone));
    }

    /**
     * 修改学生
     * @param student
     * @return
     */
    @PostMapping("/updateStudent")
    public R updateStudent(@Validated(Update.class) Student student) {
        return studentService.updateStudent(student);
    }

    /**
     * 添加学生
     *
     * @param student
     * @return
     */
    @PostMapping("/addStudent")
    public R addStudent(@Validated(Update.class)Student student) {
        return studentService.addStudent(student);
    }
    
    /**
     * 通过ID物理删除学生
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById")
    public R deleteById(int id) {
        return R.ok("删除成功！", studentService.removeById(id));
    }
    
    /**
     * 通过IDs批量物理删除学生
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds")
    public R deleteByIds(@RequestParam(value = "ids", required = false) List<Integer> ids) {
        return R.ok("批量删除成功！", studentService.removeByIds(ids));
    }

    /**
     * 修改停启用状态
     * @param id
     * @return
     */
    @PostMapping("/statusUpdate")
    public R statusUpdate(Integer id) {
        return studentService.statusUpdate(id);
    }
    
    
    /**
     * 获取学生密码
     * @param studentId
     * @return
     */
    @GetMapping("getStudentPassword")
    public R getStudentPassword(String studentId){
        return R.ok(studentService.getStudentPassword(studentId));
    }
    
    /**
     * 个人中心修改个人信息
     *
     * @param student
     * @return
     */
    @PutMapping
    public R updateStudentById(@RequestBody EmploymentInfoVo student) {
        studentService.updateStudent(student);
        return R.ok("修改成功");
    }
    
    /**
     * 个人中心修改学生头像
     * @param id
     * @param avatar
     * @return
     */
    @PutMapping("avatar")
    public R updateStudentAvatar(Integer id,String avatar) {
        Student student = new Student();
        student.setId(id);
        student.setAvatar(avatar);
        studentService.updateById(student);
        return R.ok("修改成功");
    }

    /**
     *
     * 根据学生表的学号和凭证表的凭证登录
     * @param studentId
     * @param voucher
     * @return
     */

    @PostMapping("/login")
    public R login(String studentId, String voucher){
        return this.studentService.login(studentId,voucher);
    }



    @GetMapping("/checkPhone")
    public R checkPhone(@RequestParam("telephone") String telephone){
        return R.ok(studentService.checkPhone(telephone));
    }

    @PostMapping("/sendCode") //不用get请求，因为我们需要传递手机号码
    public R sendCode(String telephone){
      return R.ok(this.studentService.sendCode(telephone));
    }

    /**
     * 注冊綁定手機發送信息
     * @param telephone
     * @return
     */
    @PostMapping("/newSendCode")
    public R newSendCode(String telephone){
        return R.ok(this.studentService.newSendCode(telephone));
    }

    /**
     * 通过学生表里的手机号和验证码登录
     * @param telephone
     * @param code
     * @return
     */

    @PostMapping("/login2")
    public R login2(String telephone,String code){
        return this.studentService.login2(telephone,code);
    }



    /**
     * 根据微信凭证登录
     *
     * @param
     * @return
     * @throws IOException
     */

    @PostMapping("/bindStudentId")
    public R bindStudentId(String studentId ,String password ,String openId) {
       return R.ok(this.studentVoucherService.bindStudentId(studentId,password,openId));
    }

    @GetMapping("/wxlogin")
    public R wxLogin(String code, String state, HttpSession session) throws IOException {
        return this.studentService.login3(code,session);
    }



    /**
     * 查询班级的所有考勤
     * @param classId
     * @return
     */
    @GetMapping("/selectAttendance")
    public R selectAttendance(Integer classId){
        return R.ok(studentService.selectAttendance(classId));
    }
    
    /**
     * 绑定微信
     * @param studentId
     * @param code
     * @param state
     * @param session
     * @return
     */
    @GetMapping("bindingWeChat")
    public R bindWeChat(String studentId, String code, String state, HttpSession session) throws IOException {
        studentService.bindingWeChat(studentId,code,state,session);
        return R.ok("绑定成功");
    }

    /**
     * 绑定手机
     * @param studentId
     * @param telephone
     * @return
     */
    @PostMapping("bindingPhone")
    public R checkPw(String studentId,String password,String telephone) {
        return R.ok(studentService.bindingPhone(studentId,password,telephone));
    }

    @PostMapping("checkCode")
    public R checkCode(String telephone,String code) {
        return R.ok(studentService.checkCode(telephone,code));
    }

}
